package com.bdth.bdthiss.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bdth.bdthiss.entity.User;
import com.bdth.bdthiss.entity.UserContract;
import com.bdth.bdthiss.service.UserContractService;
import com.bdth.bdthiss.vo.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户关注的合同 前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-09-06
 */
@Controller
@RequestMapping("/userContract")
public class UserContractController extends BaseController{
    @Autowired
    private UserContractService userContractService;

    /**
     * 关注
     * @param request
     * @param ids
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public MessageResult saveList(HttpServletRequest request, String ids){
        User session = (User)request.getSession().getAttribute("loginUser");
        String[] contractarrid = ids.split(",");
        List<UserContract>  list = new ArrayList<>();
        for(String id:contractarrid){
            UserContract temp = new UserContract(session.getId(),Integer.parseInt(id));
            list.add(temp);
        }
        userContractService.insertBatch(list);
        return MessageResult.getSuccess("创建成功");
    }

    @RequestMapping("remove")
    @ResponseBody
    public MessageResult removeList(HttpServletRequest request, String ids){
        User session = (User)request.getSession().getAttribute("loginUser");
        String[] contractarrid = ids.split(",");
        List<UserContract>  list = new ArrayList<>();
        for(String id:contractarrid){
            EntityWrapper<UserContract> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("userid",session.getId());
            entityWrapper.eq("contractid",Integer.parseInt(id));
            userContractService.delete(entityWrapper);
        }

        return MessageResult.getSuccess("取消成功");
    }
}

